package com.ts.crawler.domain;

import com.ts.crawler.common.CrawlerInfo;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.model.HttpRequestBody;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;
import us.codecraft.webmagic.utils.HttpConstant;

public class CtripPageProcesser implements PageProcessor {

    private static String URL = "https://flights.ctrip.com/international/search/api/search";

    private Site site = Site.me().setSleepTime(1000).setTimeOut(10000).setCharset("utf-8")
            .addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36");


    public void process(Page page) {


//        HashMap<String,Object> params = new HashMap();
//        HashMap<String,Object> extGlobalSwitches = new HashMap();
//        HashMap<String,Object> flightSegments = new HashMap();
//        extGlobalSwitches.put("useAllRecommendSwitch",true);
//        extGlobalSwitches.put("unfoldPriceListSwitch",true);
//        flightSegments.put("")
//        params.put("flightWayEnum","OW");
//        params.put("arrivalProvinceId",0);
//        params.put("extGlobalSwitches",extGlobalSwitches);
//        params.put("infantCount",0);
//        params.put("cabin","Y_S");
//        params.put("cabinEnum","Y_S");
//        params.put("departCountryName","中国");
//        params.put("arrivalCountryName","日本");
        //page = new Page();
        page.putField("flightResult",new JsonPathSelector("$").select(page.getRawText()));
    }

    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        Request req = new Request();
        req.setMethod(HttpConstant.Method.POST);
        req.setUrl(URL+"/batchSearch?v="+(Math.random()*1));
        req.setRequestBody(HttpRequestBody.json("{\"flightWayEnum\":\"OW\",\"arrivalProvinceId\":0,\"extGlobalSwitches\":{\"useAllRecommendSwitch\":true,\"unfoldPriceListSwitch\":true},\"arrivalCountryName\":\"日本\",\"infantCount\":0,\"cabin\":\"Y_S\",\"cabinEnum\":\"Y_S\",\"departCountryName\":\"中国\",\"flightSegments\":[{\"departureDate\":\"2021-10-11\",\"arrivalProvinceId\":0,\"arrivalCountryName\":\"日本\",\"arrivalCountryCode\":\"JP\",\"departureCityName\":\"长沙\",\"departureCityCode\":\"CSX\",\"departureCountryName\":\"中国\",\"departureCountryCode\":\"CN\",\"arrivalCityName\":\"东京\",\"arrivalCityCode\":\"TYO\",\"departureCityTimeZone\":480,\"arrivalCountryId\":78,\"timeZone\":480,\"departureCityId\":206,\"departureCountryId\":1,\"arrivalCityTimeZone\":540,\"departureProvinceId\":21,\"arrivalCityId\":228}],\"childCount\":0,\"segmentNo\":1,\"scope\":\"i\",\"adultCount\":1,\"extensionAttributes\":{\"LoggingSampling\":false,\"isFlightIntlNewUser\":false},\"transactionID\":\"9c5235370db54eb08c5058e848e759bc\",\"directFlight\":false,\"departureCityId\":206,\"isMultiplePassengerType\":0,\"noRecommend\":false,\"flightWay\":\"S\",\"arrivalCityId\":228,\"departProvinceId\":21}", "utf-8"));
        Spider.create(new SearchIdProcesser())
                .addRequest(req)
                .addPipeline(new ConsolePipeline())
                .run();

        Request req2 =new Request();
        req2.setMethod(HttpConstant.Method.POST);
        req2.setUrl(URL+"/pull/"+CrawlerInfo.searchId+"?v="+(Math.random()*1));

        req2.setRequestBody(HttpRequestBody.json("{\"flightWayEnum\":\"OW\",\"arrivalProvinceId\":0,\"extGlobalSwitches\":{\"useAllRecommendSwitch\":true,\"unfoldPriceListSwitch\":true},\"arrivalCountryName\":\"日本\",\"infantCount\":0,\"cabin\":\"Y_S\",\"cabinEnum\":\"Y_S\",\"departCountryName\":\"中国\",\"flightSegments\":[{\"departureDate\":\"2021-10-11\",\"arrivalProvinceId\":0,\"arrivalCountryName\":\"日本\",\"arrivalCountryCode\":\"JP\",\"departureCityName\":\"长沙\",\"departureCityCode\":\"CSX\",\"departureCountryName\":\"中国\",\"departureCountryCode\":\"CN\",\"arrivalCityName\":\"东京\",\"arrivalCityCode\":\"TYO\",\"departureCityTimeZone\":480,\"arrivalCountryId\":78,\"timeZone\":480,\"departureCityId\":206,\"departureCountryId\":1,\"arrivalCityTimeZone\":540,\"departureProvinceId\":21,\"arrivalCityId\":228}],\"childCount\":0,\"segmentNo\":1,\"scope\":\"i\",\"adultCount\":1,\"extensionAttributes\":{\"LoggingSampling\":false,\"isFlightIntlNewUser\":false},\"transactionID\":\"9c5235370db54eb08c5058e848e759bc\",\"directFlight\":false,\"departureCityId\":206,\"isMultiplePassengerType\":0,\"noRecommend\":false,\"flightWay\":\"S\",\"arrivalCityId\":228,\"departProvinceId\":21}", "utf-8"));
        Spider.create(new CtripPageProcesser())
                .addRequest(req2)
                .addPipeline(new ConsolePipeline())
                .run();
    }



}
